import os
import unittest
import ddt
from page.reception.index_page import IndexPage
from common.functions import browser_operation
from page.reception.login_page import LoginPage
from common.data_operation import DataOperation

# TODO 测试异常登录数据
project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
path = os.path.join(project_path, 'data/userinfo_02.xlsx')
# 创建DataOperation对象
account_list = DataOperation(path).get_data_to_dict()



@ddt.ddt
class LoginTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        driver = browser_operation()
        # 创建对象
        cls.login = LoginPage(driver)
        cls.index = IndexPage(driver)


    @ddt.data(*account_list)
    def test_login(self, data):
        # 请求网址
        self.login.get_website(self.login.login_url)
        # 隐式等待
        self.login.implicitly_wait()
        # 获取账号输入框输入账号
        self.login.input_username(data['username'])
        # 获取密码输入框输入密码
        self.login.input_password(data['password'])
        # 勾选保存密码框
        self.login.remember()
        # 获取登录按钮并点击
        self.login.btn_login()

        # 获取登录失败
        username = self.login.get_login_text()
        # 断言
        self.assertNotEqual(data["username"], username)

    def tearDown(self) -> None:
        # 登陆失败后点击注册
        self.index.reg_btn()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.login.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
